package com.library.management.controller.borrow;

import  com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.management.common.Result;
import com.library.management.service.borrow.BorrowService;
import com.library.management.domain.borrow.vo.BorrowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/borrow")
public class BorrowController {
    
    @Autowired
    private BorrowService borrowService;
    
    /**
     * 分页查询借阅记录
     */
    @GetMapping("/list")
    public Result<Page<BorrowVO>> getBorrowList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Long userId) {
        try {
            Page<BorrowVO> result = borrowService.getBorrowPage(page, size, keyword, status, userId);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询借阅记录失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据用户ID查询借阅记录
     */
    @GetMapping("/user/{userId}")
    public Result<Page<BorrowVO>> getBorrowByUserId(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<BorrowVO> result = borrowService.getBorrowByUserId(userId, page, size);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询用户借阅记录失败: " + e.getMessage());
        }
    }
    
    /**
     * 归还图书
     */
    @PostMapping("/return/{borrowId}")
    public Result<String> returnBook(
            @PathVariable Long borrowId,
            @RequestParam(required = false) String remarks) {
        try {
            boolean success = borrowService.returnBook(borrowId, remarks);
            if (success) {
                return Result.success("归还成功");
            } else {
                return Result.error("归还失败");
            }
        } catch (Exception e) {
            return Result.error("归还失败: " + e.getMessage());
        }
    }
    
    /**
     * 续借图书
     */
    @PostMapping("/renew/{borrowId}")
    public Result<String> renewBook(
            @PathVariable Long borrowId,
            @RequestParam Integer renewDays) {
        try {
            boolean success = borrowService.renewBook(borrowId, renewDays);
            if (success) {
                return Result.success("续借成功");
            } else {
                return Result.error("续借失败");
            }
        } catch (Exception e) {
            return Result.error("续借失败: " + e.getMessage());
        }
    }
    
    /**
     * 借阅图书
     */
    @PostMapping("/create")
    public Result<String> borrowBook(@RequestBody Map<String, Object> params) {
        try {
            Long userId = Long.valueOf(params.get("userId").toString());
            Long bookId = Long.valueOf(params.get("bookId").toString());
            Integer borrowDays = Integer.valueOf(params.get("borrowDays").toString());
            String remarks = (String) params.get("remarks");
            
            boolean success = borrowService.borrowBook(userId, bookId, borrowDays, remarks);
            if (success) {
                return Result.success("借阅成功");
            } else {
                return Result.error("借阅失败");
            }
        } catch (Exception e) {
            return Result.error("借阅失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新借阅状态
     */
    @PostMapping("/update-status")
    public Result<String> updateBorrowStatus() {
        try {
            borrowService.updateBorrowStatus();
            return Result.success("状态更新成功");
        } catch (Exception e) {
            return Result.error("状态更新失败: " + e.getMessage());
        }
    }
} 